package org.aplikator.client.panels;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;

public class ContentsPanel extends ResizeComposite {
    public interface ContentsPanelResources extends ClientBundle {
        public static final ContentsPanelResources INSTANCE = GWT.create(ContentsPanelResources.class);

        @Source("ContentsPanel.css")
        public ContentsPanelCss css();
    }

    public interface ContentsPanelCss extends CssResource {
        public String contentsHeader();

        public String contentsFrame();

        public String contentsHolder();
    }

    static {
        ContentsPanelResources.INSTANCE.css().ensureInjected();
    }

    private FlowPanel headerPanel = new FlowPanel();
    private LayoutPanel contents = new LayoutPanel();
    private Label headerText = new Label();
    private DockLayoutPanel holder = new DockLayoutPanel(Unit.EM);

    public ContentsPanel() {

        headerPanel.add(headerText);
        // headerPanel.setWidth("100%");
        // headerPanel.setHeight("2em");
        // headerPanel.getElement().getStyle().setPosition(Position.RELATIVE);
        // contents.getElement().getStyle().setPosition(Position.RELATIVE);
        // ScrollPanel scroller = new ScrollPanel(contents);
        // contents.setSize("100%", "100%");

        holder.addNorth(headerPanel, 2);
        holder.add(contents);

        initWidget(holder);

        headerPanel.setStyleName(ContentsPanelResources.INSTANCE.css().contentsHeader());
        holder.setStyleName(ContentsPanelResources.INSTANCE.css().contentsFrame());
        contents.setStyleName(ContentsPanelResources.INSTANCE.css().contentsHolder());

    }

    public void setContents(Widget wdgt) {
        contents.clear();
        contents.add(wdgt);
    }

    public void setTitle(String title) {
        headerText.setText(title);
    }

    /**
     * Adjusts the widget's size such that it fits within the window's client
     * area.
     */
    /*
     * public void adjustSize(int windowWidth, int windowHeight) { int
     * scrollWidth = windowWidth - holder.getAbsoluteLeft() - 3; if (scrollWidth
     * < 1) { scrollWidth = 1; }
     * 
     * int scrollHeight = windowHeight - holder.getAbsoluteTop() - 3; if
     * (scrollHeight < 1) { scrollHeight = 1; }
     * 
     * holder.setPixelSize(scrollWidth, scrollHeight); }
     */
}
